/**
 * 获取表单行数据
 * @param tableId
 * @param id
 */
async function getRowItemById(tableId, id) {
    var rowData = await new Promise(function(resolve, reject){
        layui.use("table", function () {
            var table = layui.table;

            var data = table.cache[tableId];
            for(var i = 0; i<data.length; i++){
                var value = data[i];
                if (parseInt(id) === parseInt(value.id) || parseInt(id) === parseInt(value.seqNo)) {
                    resolve(value);
                }
            }
        });
    }).then((result) => {
        return result;
    });

    return rowData;
}

/**
 * 获取表单行数据
 */
async function getRowItemBySeqNo(tableId, id){
    var rowdata = await new Promise(function(resolve, reject){
        layui.use("table", function () {
            var table = layui.table;

            var data = table.cache[tableId];
            var target = "";
            for(var i = 0; i<data.length; i++){
                var value = data[i];
                if (parseInt(id) === parseInt(value.seqNo)) {
                    resolve(value);
                    return;
                }
            }
            resolve({});
        });
    }).then(function(result){
        debugger
        return result;
    });
    return rowdata;
}


/**
 * 更新采购订单商品信息
 * @param tableId
 * @param rowdata
 */
function updateRowItemByRow(tableId, rowdata) {
    layui.use("table", function () {
        var table = layui.table;
        var data = table.cache[tableId];
        var newdata = [];
        data.forEach(function (value, index, array) {
            if (parseInt(value.id) === parseInt(rowdata.id) || parseInt(value.seqNo) === parseInt(rowdata.seqNo)) {
                //更新新的信息
                value.goodCode = rowdata.goodCode;
                value.varietyCode = (typeof(rowdata.varietyCode) == 'undefined' ? '' : rowdata.varietyCode);
                value.varietyName = (typeof(rowdata.varietyName) === 'undefined' ? '' : rowdata.varietyName);
                value.standardName = (typeof(rowdata.standardName) === 'undefined' ? '' : rowdata.standardName);
                value.unit = (typeof(rowdata.unit) === 'undefined' ? '' : rowdata.unit);
                value.inTaxRate = (typeof(rowdata.inTaxRate) === 'undefined' ? "0" : rowdata.inTaxRate);
                value.price = (typeof(rowdata.price) === 'undefined' ? "0" : rowdata.price);
                value.noTaxPrice = (typeof(rowdata.noTaxPrice) === 'undefined' ? "0" : rowdata.noTaxPrice);
                value.amount = (typeof(rowdata.amount) === 'undefined' ? "0" : rowdata.amount);
                value.bal = (typeof(rowdata.bal) === 'undefined' ? "0" : rowdata.bal);
                value.noTaxBal = (typeof(rowdata.noTaxBal) === 'undefined' ? "0" : rowdata.noTaxBal);
                value.estInTax = (typeof(rowdata.estInTax) === 'undefined' ? "0" : rowdata.estInTax);
                value.digest = (typeof(rowdata.digest) === 'undefined' ? "" : rowdata.digest);
                newdata.push(value);
            } else {
                newdata.push(value);
            }
        });

        table.reload(tableId, {
            data: newdata
        });
    });
}

function updateRowItem(tableId, rowdata, isClear){
    if(!isClear){
        return updateRowItemByRow(tableId, rowdata);
    }

    layui.use("table", function () {
        var table = layui.table;
        var data = table.cache[tableId];
        var newdata = [];
        data.forEach(function (value, index, array) {
            if (parseInt(value.id) === parseInt(rowdata.id) || parseInt(value.seqNo) === parseInt(rowdata.seqNo)) {
                //更新新的信息
                value = {};
                value.id = rowdata.id;
                value.seqNo = rowdata.seqNo;
                newdata.push(value);
            } else {
                newdata.push(value);
            }
        });

        table.reload(tableId, {
            data: newdata
        });
    });
}